package View;


import database.BookData;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.*;
import java.sql.Date;
import java.util.Vector;

public class ChangeBook extends JFrame {

    private String book;
    private String author;
    private int id;
    public JLayeredPane laypane1 = new JLayeredPane();
    private DefaultTableModel model;
    private String select ;
    private JCheckBox box;
    private int selectrow;
    private int selectcolumn;
    public int bookid;//书的编号
    public Date borrowtime;//借阅时间
    private String borrower;//借阅人
    public String lend;//是否在馆
    public String bookname;//书名
    private String bookname1;//书名1
    private String bookname2;//书名2
    DefaultTableModel model1;
    private String borroww;
    private String returnn;

    public void setName(String name) {
        this.borrower = name;

    }

    public void setModel(DefaultTableModel model1) {
        this.model1 = model1;
    }

    public ChangeBook()  {

        /*
         * 查询图书
         */
        Font font1 = new Font("宋体",Font.BOLD,13);
        Font font2 = new Font("宋体",Font.BOLD,20);




        /*面板3中的标签*/
        Font font = new Font("宋体",Font.BOLD,20);
        JLabel lab3 = new JLabel("请选择查询方式：");
        lab3.setFont(font);
        lab3.setBounds(140, 60, 600, 30);


        /*面板3中的查询下拉框*/
        Dimension dimension = new Dimension(220,30);
        JComboBox<String> j = new JComboBox<>();
        j.setSize(dimension);
        j.addItem("按照书名查询");
        j.addItem("按照作者查询");
        j.addItem("按照编号查询");
        j.setBounds(140, 100,220, 30);
        j.setBackground(new Color(51,153,255));
        j.setFont(font1);


        /*面板3中的搜索框*/
        JTextField text3 = new JTextField();
        Dimension dimension1 = new Dimension(270,30);
        text3.setFont(font1);
        text3.setSize(dimension1);
        text3.setBackground(new Color(51,153,255));
        text3.setBounds(390,100, 270, 30);

        /*按钮*/
        JButton button = new JButton("搜索");
        button.setBackground(new Color(51,153,255));
        button.setBounds(670, 100, 120, 30);
        button.setFont(font2);

        /*新建表格*/
        model = new DefaultTableModel();
        model.addColumn("书本编号", new Vector<Integer>());
        model.addColumn("书名",new Vector<String>());
        model.addColumn("作者",new Vector<String>());
        model.addColumn("是否在馆",new Vector<String>());
        model.addColumn("选择图书", new Vector<String>());
        JTable table = new JTable(model);

        JScrollPane jp = new JScrollPane(table);
        jp.setBounds(100, 150, 750, 450);

        /*显示出全部图书*/
        BookData bd = new BookData();
        try {

            bd.selectAll(model);
        } catch (Exception e2) {

            e2.printStackTrace();
        }


        JTableHeader head = table.getTableHeader();//创建表格表头对象
        head.setPreferredSize(new Dimension(head.getWidth(),35));//设置表头框框大小
        head.setFont(new Font("宋体",Font.BOLD,15));//设置表格字体
        table.setRowHeight(30);//设置表格行宽
        table.setFont(new Font("宋体", Font.PLAIN,13));//设置表格行中字体大小

        /*使单元格中的内容居中*/
        DefaultTableCellRenderer renderer=new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        table.setDefaultRenderer(Object.class, renderer);

        /*添加复选框*/
        table.getColumnModel().getColumn(4).setCellRenderer(new TableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                                                           boolean isSelected, boolean hasFocus,int row, int column) {
                /*定义复选框*/
                box = new JCheckBox();
                /*使具有焦点的行对应的复选框被选中*/
                box.setSelected(isSelected);

                /*使复选框在表格中居中*/
                box.setHorizontalAlignment((int)0.5f);
                return box;
            }
        });



        JButton button1 = new JButton("添加");
        JButton button2 = new JButton("删除");
        JButton button3 = new JButton("修改");
        button1.setFont(font2);
        button2.setFont(font2);
        button3.setFont(font2);
        button1.setBackground(new Color(51,153,255));
        button1.setBounds(160, 630, 170, 40);
        button2.setBackground(new Color(51,153,255));
        button2.setBounds(405,630, 170, 40);
        button3.setBackground(new Color(51,153,255));
        button3.setBounds(630,630, 170, 40);

        /*把组件放在分层窗格里*/

        laypane1.add(lab3, 150,1);
        laypane1.add(j, 100,2);
        laypane1.add(text3, 100,3);
        laypane1.add(button, 100,4);
        laypane1.add(jp, 200, 5);
        laypane1.add(button1, 630,6);
        laypane1.add(button2, 630,7);
        laypane1.add(button3, 630,8);


        /*下拉框添加事件*/
        select = "按照书名查询";
        j.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {

                if(e.getStateChange()==ItemEvent.SELECTED) {
                    select =(String) e.getItem();

                }
            }
        });

        /*搜索监听*/
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                model.setRowCount(0);
                if(select.equals("按照书名查询")) {
                    book = text3.getText().trim();
                    try {

                        bd.bookSelect(model,book);
                    } catch (Exception e1) {

                        e1.printStackTrace();
                    }

                }else if(select.equals("按照作者查询")) {
                    author = text3.getText().trim();
                    try {

                        bd.authorSelect(model,author);
                    } catch (Exception e1) {

                        e1.printStackTrace();
                    }

                }else if(select.equals("按照编号查询")) {
                    id = Integer.parseInt(text3.getText().trim());
                    try {

                        bd.idSelect(model,id);
                    } catch (Exception e1) {

                        e1.printStackTrace();
                    }
                }
            }
        });

        /*选中行监听*/
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectrow = table.getSelectedRow();
                bookid =  (int) table.getValueAt(selectrow, 0);//获取所选书本编号
                bookname = (String) table.getValueAt(selectrow, 1);//获取所选书名
                lend= (String) table.getValueAt(selectrow, 3);//获取所选书本状态

            }
        });

        borroww = "借出";
        returnn = "退还";


        /*添加图书*/
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new AddBook(model);

            }
        });

        /*修改图书*/
        button3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new FixBook(model,bookid);

            }
        });

        /*删除图书*/
        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                new DeleteBook(model,bookid,bookname);
            }
        });

        laypane1.setOpaque(false);

        text3.setOpaque(false);
    }

}
